package com.navinfo.platform.hbase.sample;

import com.navinfo.platform.hbase.api.HbaseTemplate;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

import java.util.ArrayList;
import java.util.List;

public class HbaseSample {
    public static void main(String[] args)
    {
        //1.HBaseConfiguration配置
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "10.30.10.11,10.30.10.12,10.30.10.13");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        configuration.set("zookeeper.znode.parent",  "/hbase-unsecure");
        HbaseTemplate ht = new HbaseTemplate(configuration);

        //2.进行批量的SaveOrUpdateorDelete
        List<Mutation> saveOrUpdates = new ArrayList<>();

        //插入或更新数据
        Put put = new Put(Bytes.toBytes("135xxxxxx"));
        put.addColumn(Bytes.toBytes("people"), Bytes.toBytes("name"), Bytes.toBytes("test"));
        saveOrUpdates.add(put);

        //删除数据
        Delete delete = new Delete(Bytes.toBytes("136xxxxxx"));
        saveOrUpdates.add(delete);

        ht.saveOrUpdates("people_table", saveOrUpdates);

        //3.查询
        //使用scan批量查询
        Scan scan = new Scan(Bytes.toBytes("#"), Bytes.toBytes("}"));
        scan.setCaching(5000);
        List<PeopleDto> dtos = ht.find("people_table", scan, new PeopleRowMapper());

        System.out.println("---dtos----------start-----------");
        for(PeopleDto dd : dtos) {
            System.out.println(dd.toString());
        }
        System.out.println("---dtos----------end-----------");

        //使用get获取某行数据
        PeopleDto dto = ht.get("people_table", "135xxxxxx", new PeopleRowMapper());
        System.out.println("---dto----------start-----------");
        System.out.println(dto.toString());
        System.out.println("---dto----------end-----------");

        System.exit(0);


    }
}
